package com.bop.dehong.login;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.base.dehong.http.RequestCallBack;
import com.base.dehong.util.RegexChk;
import com.base.dehong.util.StringUtils;
import com.base.dehong.util.ToastUtils;
import com.bop.dehong.R;
import com.bop.dehong.base.BaseActivity;
import com.bop.dehong.base.TopManager;
import com.bop.dehong.config.Config;
import com.bop.dehong.http.model.ResetPasswordModel;
import com.bop.dehong.info.EmptyInfo;


public class ResetPasswordActivity extends BaseActivity {
	private Activity mActivity;
	private EditText mPassword, mPasswordAgain;
	private TextView mSubmit;
	private String uid, username, password;
	private ResetPasswordModel mResetModel;

	@Override
	protected void initUI() {
		mPassword = getView(R.id.reset_password);
		mPasswordAgain = getView(R.id.reset_password_again);
		mSubmit = getView(R.id.reset_submit);
		mSubmit.setOnClickListener(this);
		Intent intent = getIntent();
		uid = intent.getStringExtra(Config.CODE);
		username = intent.getStringExtra("username");
	}

	@Override
	protected void initTop() {
		View mView = getView(R.id.reset_top);
		TopManager manager = new TopManager(mView);
		manager.setLeftImageVisibility(View.VISIBLE);
		manager.setTitle("重置密码");
	}
	
	@Override
	public void onTitleLeftClick(View v) {
		getExitDialog();
	}
	
	 @Override
	 public boolean onKeyDown(int keyCode, KeyEvent event)
	 {
	     if (keyCode == KeyEvent.KEYCODE_BACK)
	      {
	    	 	getExitDialog();
	            return true;
	      }
	      return super.onKeyDown(keyCode, event);
	 }
	
	 Dialog mExitDialog;
	/*
	 * 退出对话框
	 */
	private void getExitDialog() {
		if (mExitDialog == null) {
			mExitDialog = new Dialog(this, R.style.MyDialogStyle);
			View view = View.inflate(this, R.layout.dialog_forget_pwd, null);
			TextView contentView = getView(view, R.id.textView2);
			contentView.setText("确认放弃找回密码吗？");
			Button mOKBtn = getView(view, R.id.re_login);
			mOKBtn.setText("确定");
			mExitDialog.setContentView(view);
			mExitDialog.setCanceledOnTouchOutside(true);
			registerOnClickListener(mOKBtn,getView(view, R.id.re_cancel));
		}
		if (!mExitDialog.isShowing()) {
			mExitDialog.show();
		}
	}
	
	private void resetPwd(){
		password = getStr4TextView(mPassword);
		if (StringUtils.isEmpty(password)) {
			ToastUtils.show(mActivity, "密码不能为空!!!");
			return;
		}
		String passwordagain = getStr4TextView(mPasswordAgain);
		if (StringUtils.isEmpty(passwordagain)) {
			ToastUtils.show(mActivity, "确认密码不能为空!!!");
			return;
		}
		if (!RegexChk.isPwd(passwordagain)) {
			ToastUtils.show(this, "密码由大小写字母及数字组合，6-18位字符");
			return;
		}
			showProgressDialog();
			if (mResetModel == null ) {
				mResetModel = new ResetPasswordModel(this, resetCallBack);
			}
			mResetModel.doNet(password, passwordagain, uid);
	}
	
	RequestCallBack<EmptyInfo> resetCallBack = new RequestCallBack<EmptyInfo>() {

		@Override
		public void end() {
			disappearProgressDialog();
		}

		@Override
		public void onSuccess(EmptyInfo info) {
			// TODO Auto-generated method stub
			super.onSuccess(info);
			ToastUtils.show(mActivity, "修改成功!");
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
			bundle.putString("username", username);
			bundle.putString("password", password);
			intent.putExtras(bundle);
			setResult(RESULT_OK, intent);
			finishCurrentActivity();
		}
		
	};

	@Override
	protected void initData() {
		mActivity = ResetPasswordActivity.this;
	}

	@Override
	protected int getContentViewId() {
		TAG = getClass().getName();
		return R.layout.activity_find_password;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (mExitDialog != null) {
			mExitDialog.dismiss();
			mExitDialog = null;
		}
	}

	@Override
	public void onClick(View v)
	{
		switch (v.getId()) {
		case R.id.reset_submit:
			resetPwd();
			break;
		case R.id.re_login:
			if (mExitDialog != null) {
				mExitDialog.dismiss();
			}
			finishCurrentActivity();
			break;
		case R.id.re_cancel:
			if (mExitDialog != null) {
				mExitDialog.dismiss();
			}
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id)
	{
		// TODO Auto-generated method stub
		
	}
}
